package eliauk.fun.flowabledemo.listener;

import cn.hutool.core.util.StrUtil;
import org.flowable.common.engine.api.delegate.event.*;
import org.flowable.common.engine.impl.event.FlowableEntityEventImpl;
import org.flowable.engine.TaskService;
import org.flowable.engine.delegate.event.impl.FlowableEntityWithVariablesEventImpl;
import org.flowable.engine.impl.persistence.entity.DeploymentEntity;
import org.flowable.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.flowable.engine.impl.util.CommandContextUtil;
import org.flowable.task.api.Task;
import org.flowable.task.service.impl.persistence.entity.TaskEntityImpl;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * flowable 全局监听器
 */
@Component
public class GlobalProcessInstanceListener implements FlowableEventListener {

    @Resource
    private TaskService taskService;


    /**
     * 用户任务创建完成时，将当前登录用户作为任务的委托人
     *
     * @param event the event
     */
    @Override
    public void onEvent(FlowableEvent event) {
        FlowableEventType type = event.getType();
        if (type == FlowableEngineEventType.TASK_CREATED) {
            // 用户任务创建完成
            if (event instanceof FlowableEntityEventImpl) {
                // 设置任务的委托者
                Task task = (Task) ((FlowableEntityEventImpl) event).getEntity();
                task.setOwner("是我");

                // 设置任务的类型
                FlowableEntityEvent entityEvent = (FlowableEntityEvent) event;
                TaskEntityImpl entity = (TaskEntityImpl) entityEvent.getEntity();
                if (StrUtil.isBlank(entity.getCategory())) {
                    //前提是Deployment已经设置过Category
                    ProcessDefinitionEntity processDefinitionEntity = CommandContextUtil
                            .getProcessDefinitionEntityManager()
                            .findById(entity.getProcessDefinitionId());
                    DeploymentEntity deploymentEntity = CommandContextUtil
                            .getDeploymentEntityManager()
                            .findById(processDefinitionEntity.getDeploymentId());
                    //直接修改act_ru_task实体的属性
                    //因为事件是同步事件，所以会包裹在整体事务中提交
                    entity.setCategory(deploymentEntity.getCategory());
                }
                taskService.saveTask(task);
            }
        } else if (type == FlowableEngineEventType.TASK_COMPLETED) {
            // 用户审批任务
            if (event instanceof FlowableEntityWithVariablesEventImpl) {

            }
        } else if (type == FlowableEngineEventType.PROCESS_COMPLETED) {
            // 流程结束
            if (event instanceof org.flowable.engine.delegate.event.impl.FlowableEntityEventImpl) {

            }
        } else if (type == FlowableEngineEventType.PROCESS_CANCELLED) {
            // 流程删除
            if (event instanceof org.flowable.engine.delegate.event.impl.FlowableProcessCancelledEventImpl) {

            }
        }
    }


    @Override
    public boolean isFailOnException() {
        return false;
    }

    @Override
    public boolean isFireOnTransactionLifecycleEvent() {
        return false;
    }

    @Override
    public String getOnTransaction() {
        return null;
    }
}